#!/bin/bash

# 远程服务器验证脚本
# 用法: ./verify_remote.sh

set -e

# 配置
REMOTE_USER="asr"
REMOTE_HOST="192.168.10.11"
REMOTE_PATH="/home/asr/git/paddle-ocr-webcam"
CONDA_ENV="ov"
REMOTE_URL="$REMOTE_USER@$REMOTE_HOST"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印函数
print_header() {
    echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${NC} $1"
    echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

# 主验证函数
main() {
    print_header "远程服务器验证开始"
    
    echo ""
    print_info "连接到远程服务器: $REMOTE_URL"
    print_info "项目路径: $REMOTE_PATH"
    print_info "Conda 环境: $CONDA_ENV"
    
    # 执行远程验证
    ssh "$REMOTE_URL" << 'REMOTE_SCRIPT'
        set -e
        
        # 导入颜色定义
        RED='\033[0;31m'
        GREEN='\033[0;32m'
        YELLOW='\033[1;33m'
        BLUE='\033[0;34m'
        NC='\033[0m'
        
        REMOTE_PATH="/home/asr/git/paddle-ocr-webcam"
        CONDA_ENV="ov"
        
        # 进入项目目录
        cd "$REMOTE_PATH"
        
        # 激活 conda 环境
        source /home/asr/anaconda3/etc/profile.d/conda.sh
        conda activate $CONDA_ENV
        
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${BLUE}1️⃣  代码验证${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        # 检查接口是否存在
        if grep -q "process-video-text-only" main.py; then
            echo -e "${GREEN}✅ /process-video-text-only 接口已添加${NC}"
        else
            echo -e "${RED}❌ /process-video-text-only 接口未找到${NC}"
            exit 1
        fi
        
        # 检查方法是否存在
        if grep -q "process_video_text_only" video_processor.py; then
            echo -e "${GREEN}✅ process_video_text_only() 方法已添加${NC}"
        else
            echo -e "${RED}❌ process_video_text_only() 方法未找到${NC}"
            exit 1
        fi
        
        # 检查文档文件
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${BLUE}2️⃣  文档验证${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        doc_count=0
        
        if [ -f "TEXT_ONLY_API.md" ]; then
            echo -e "${GREEN}✅ TEXT_ONLY_API.md 存在${NC}"
            ((doc_count++))
        fi
        
        if [ -f "TEXT_ONLY_QUICK_START.md" ]; then
            echo -e "${GREEN}✅ TEXT_ONLY_QUICK_START.md 存在${NC}"
            ((doc_count++))
        fi
        
        if [ -f "TEXT_ONLY_FEATURE_SUMMARY.md" ]; then
            echo -e "${GREEN}✅ TEXT_ONLY_FEATURE_SUMMARY.md 存在${NC}"
            ((doc_count++))
        fi
        
        if [ -f "NEW_FEATURE_IMPLEMENTATION.md" ]; then
            echo -e "${GREEN}✅ NEW_FEATURE_IMPLEMENTATION.md 存在${NC}"
            ((doc_count++))
        fi
        
        echo -e "${GREEN}✅ 文档文件总数: $doc_count${NC}"
        
        # 检查 Git 提交
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${BLUE}3️⃣  Git 提交验证${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        echo -e "${BLUE}最新 5 个提交:${NC}"
        git log --oneline -5 | sed 's/^/  /'
        
        # 检查是否有新的提交
        if git log --oneline -1 | grep -q "feat: 添加文本识别专用接口"; then
            echo -e "${GREEN}✅ 新功能提交已找到${NC}"
        else
            echo -e "${YELLOW}⚠️  未找到新功能提交，但代码已更新${NC}"
        fi
        
        # 检查 Python 语法
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${BLUE}4️⃣  Python 语法验证${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        if python -m py_compile main.py 2>/dev/null; then
            echo -e "${GREEN}✅ main.py 语法正确${NC}"
        else
            echo -e "${RED}❌ main.py 语法错误${NC}"
            exit 1
        fi
        
        if python -m py_compile video_processor.py 2>/dev/null; then
            echo -e "${GREEN}✅ video_processor.py 语法正确${NC}"
        else
            echo -e "${RED}❌ video_processor.py 语法错误${NC}"
            exit 1
        fi
        
        # 检查依赖
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${BLUE}5️⃣  依赖验证${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        required_packages=("fastapi" "starlette" "cv2" "numpy" "paddle")
        
        for package in "${required_packages[@]}"; do
            if python -c "import $package" 2>/dev/null; then
                echo -e "${GREEN}✅ $package 已安装${NC}"
            else
                echo -e "${YELLOW}⚠️  $package 未安装${NC}"
            fi
        done
        
        # 总结
        echo ""
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        echo -e "${GREEN}✅ 所有验证项目已完成${NC}"
        echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
        
        echo ""
        echo -e "${BLUE}下一步:${NC}"
        echo "  1. 启动服务: python main.py"
        echo "  2. 测试接口: curl -X POST http://localhost:8000/process-video-text-only -F 'file=@test.mp4'"
        echo "  3. 查看文档: cat TEXT_ONLY_QUICK_START.md"
        
REMOTE_SCRIPT
    
    echo ""
    print_header "远程服务器验证完成"
}

# 错误处理
trap 'print_error "验证过程中出错"; exit 1' ERR

# 运行主函数
main "$@"

